import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class SearchPage extends StatefulWidget {
  const SearchPage({Key key}) : super(key: key);

  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  int childCount = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverAppBar(
            pinned: true,
            title: Align(
              alignment: Alignment.center,
              child: Container(
                height: 36.0,
                width: double.infinity,
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(5)),
                child: TextField(
                  keyboardType: TextInputType.text,
                  autofocus: false,
                  decoration: InputDecoration(
                    //输入里面输入文字内边距设置
                    contentPadding: EdgeInsets.only(left: 12.0),
                    hintText: '留下你的精彩评论吧',
                    hintStyle: TextStyle(
                      fontSize: 14,
                    ),
                    counterText: '',
                    //不需要输入框下划线
                    border: InputBorder.none,
                    suffixIcon: IconButton(
                      onPressed: () {
                        setState(() {
                          childCount = 20;
                        });
                      },
                      padding: EdgeInsets.all(0.0),
                      icon: Icon(Icons.search, size: 24.0),
                    ),
                  ),
                  onChanged: (value) {
                    if (value.length == 0) {
                      setState(() {
                        childCount = 0;
                      });
                    }
                  },
                  maxLength: 18,
                  inputFormatters: [LengthLimitingTextInputFormatter(18)],
                ),
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return Container(
                  height: 50.0,
                  color: index.isOdd ? Colors.white : Colors.blue[200],
                  child: Center(
                    child: Text('Item $index'),
                  ),
                );
              },
              childCount: childCount,
            ),
          ),
        ],
      ),
    );
  }
}
